package com.yupi.moshu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yupi.moshu.common.BaseResponse;
import com.yupi.moshu.common.ErrorCode;
import com.yupi.moshu.common.ResultUtils;
import com.yupi.moshu.mapper.DocumentMapper;
import com.yupi.moshu.model.domain.Document;
import com.yupi.moshu.model.request.*;
import com.yupi.moshu.service.DocumentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文档管理接口
 * @author 7
 */
@RestController
@RequestMapping("/document")
public class documentController {
    @Resource
    private DocumentService documentService;

    @Resource
    private DocumentMapper documentMapper;

    @PostMapping(value = "/new")
    public BaseResponse<Void> newDocument(@RequestBody SaveDocRequest saveDocRequest) {
        Document document = new Document();
        document.setTitle(saveDocRequest.getTitle());
        document.setContent(saveDocRequest.getContent());
        document.setTeamID(saveDocRequest.getTeamID());
        document.setProjectID(saveDocRequest.getProjectID());
        document.setIsLocked(0);
        documentService.save(document);

        return ResultUtils.success("文档已保存");
    }

    @PostMapping(value = "/view")
    public BaseResponse<Document> viewDocument(@RequestBody LoadDocRequest loadDocRequest) {
        Document document = documentService.getById(loadDocRequest.getDocID());
        return ResultUtils.success(document);
    }

    @PostMapping(value = "/viewDocList")
    public BaseResponse<List<Document>> newDocument(@RequestBody DocListRequest docListRequest) {
        QueryWrapper<Document> documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("projectID", docListRequest.getProjectID());
        List<Document> documentList = documentService.list(documentQueryWrapper);
        return ResultUtils.success(documentList);
    }

    @PostMapping(value = "/lockDocument")
    public BaseResponse<Void> lockDoc(@RequestBody LockDocumentRequest lockDocumentRequest) {

        Document document=documentService.getById(lockDocumentRequest.getDocID());
        if (document.getIsLocked()!=0){
            return ResultUtils.error(ErrorCode.NO_AUTH, "有他人在修改此文档，请稍等");
        }
        else {
            UpdateWrapper<Document> documentUpdateWrapper = new UpdateWrapper<>();
            documentUpdateWrapper.eq("id", lockDocumentRequest.getDocID());
            documentUpdateWrapper.set("isLocked",1);
            documentService.update(documentUpdateWrapper);
            return ResultUtils.success("请开始修改");
        }
    }

    @PostMapping(value = "/modify")
    public BaseResponse<Void> modifyDoc(@RequestBody ModifyDocRequest modifyDocRequest) {
        UpdateWrapper<Document> documentUpdateWrapper = new UpdateWrapper<>();
        documentUpdateWrapper.eq("id", modifyDocRequest.getDocID());
        documentUpdateWrapper.set("content", modifyDocRequest.getContent());
        documentUpdateWrapper.set("isLocked",0);
        documentService.update(documentUpdateWrapper);
        return ResultUtils.success("修改已保存");
    }
}
